from DeterministicAutomata import *
from Determinizer import *
from AutomataBuilder import *

def test11():
    nro_automata = 1
    a = AutomataDot(nro_automata)
    a.generate_diagram("a-dot")
    h = [] 
    for q in a.delta:
        h.insert(0, q[1])
    h.sort()
    print h

def test10():
    # (ab)+
    nro_automata = 1
    a = AutomataChar('a', nro_automata)
    nro_automata += 1

    b = AutomataChar('b', nro_automata)
    nro_automata += 1

    ab = AutomataConcat(a, b, nro_automata)
    nro_automata += 1

    ab_mas = AutomataMas(ab, nro_automata)
    ab_mas.generate_diagram("test-ab_mas")

def test9():
    # (ab)?
    nro_automata = 1
    a = AutomataChar('a', nro_automata)
    nro_automata += 1

    b = AutomataChar('b', nro_automata)
    nro_automata += 1

    ab = AutomataConcat(a, b, nro_automata)
    nro_automata += 1

    ab_opt = AutomataOptional(ab, nro_automata)

    ab_opt.generate_diagram("test-ab_opt")

def test8():
    # ab|c
    nro_automata = 1
    a = AutomataChar('a', nro_automata)
    nro_automata += 1

    b = AutomataChar('b', nro_automata)
    nro_automata += 1

    ab = AutomataConcat(a, b, nro_automata)
    nro_automata += 1

    c = AutomataChar('c', nro_automata)
    nro_automata += 1

    final = AutomataUnion(ab, c, nro_automata)
    final.generate_diagram("test-union")

def test7():
    # (ab)*c
    nro_automata = 1
    a = AutomataChar('a', nro_automata)
    nro_automata += 1

    b = AutomataChar('b', nro_automata)
    nro_automata += 1

    ab = AutomataConcat(a, b, nro_automata)
    nro_automata += 1

    ab_ast = AutomataStar(ab, nro_automata)
    nro_automata += 1

    c = AutomataChar('c', nro_automata)
    nro_automata += 1

    final = AutomataConcat(ab_ast, c, nro_automata)
    final.generate_diagram("test-groso")

def test6():
    a = AutomataBuilder()
    [ a.add_state('q' + str(p)) for p in range(0, 6) ]
    a.set_initial('q0')
    a.add_final('q5')
    a.add_transition('q0', '', 'q1')
    a.add_transition('q0', '', 'q2')
    a.add_transition('q1', 'a', 'q3')
    a.add_transition('q2', 'b', 'q4')
    a.add_transition('q3', '', 'q5')
    a.add_transition('q4', '', 'q5')

    a.generate_diagram('test-a')

    b = AutomataBuilder()
    [ b.add_state('t' + str(p)) for p in range(0, 6) ]
    b.set_initial('t0')
    b.add_final('t5')
    b.add_transition('t0', '', 't1')
    b.add_transition('t0', '', 't2')
    b.add_transition('t1', 'a', 't3')
    b.add_transition('t2', 'b', 't4')
    b.add_transition('t3', '', 't5')
    b.add_transition('t4', '', 't5')

    b.generate_diagram('test-b')

    ab = AutomataConcat(a, b, 'C')
    ab.generate_diagram("test-ab")

def test5():
    nro_automata = 1
    a = AutomataChar('a', nro_automata)
    nro_automata += 1

    b = AutomataChar('b', nro_automata)
    nro_automata += 1
    
    ab = AutomataConcat(a, b, nro_automata)
    nro_automata += 1

    c = AutomataChar('c', nro_automata)
    nro_automata += 1
    abc = AutomataConcat(ab, c, nro_automata)
    abc.generate_diagram("test-2")

def test4():
    a = AutomataBuilder()
    [ a.add_state('q' + str(p)) for p in range(0, 6) ]
    a.set_initial('q0')

    a.add_transition('q0', '' , 'q1')
    a.add_transition('q1', '1', 'q0')
    a.add_transition('q2', '0', 'q0')
    a.add_transition('q1', '' , 'q2')
    a.add_transition('q2', '1', 'q3')
    a.add_transition('q0', '2', 'q4')
    a.add_transition('q4', '1', 'q5')

    a.add_final('q3')
    a.add_final('q5')
    
    a.generate_diagram("test-0")

    det = Determinizer(a)
    d = det.determinize()
    d = det.stringuify_set_states(d)
    d.generate_diagram("test-2")

def test3():
    a = AutomataBuilder()
    [ a.add_state('q' + str(p)) for p in range(0, 8) ]
    a.set_initial('q0')

    a.add_transition('q0', '', 'q1')
    a.add_transition('q1', '1', 'q2')
    a.add_transition('q2', '0', 'q3')
    a.add_transition('q3', '1', 'q7')
    
    a.add_transition('q0', '', 'q4')
    a.add_transition('q4', '0', 'q5')
    a.add_transition('q5', '1', 'q6')
    a.add_transition('q6', '0', 'q7')
    
    a.add_transition('q7', '0', 'q7')
    a.add_transition('q7', '1', 'q7')

    a.add_final('q3')
    a.add_final('q5')
    a.add_final('q7')

    a.generate_diagram("test-0")

    det = Determinizer(a)
    d = det.determinize()
    #~ d.generate_diagram("test-1")
    d = det.stringuify_set_states(d)
    d.generate_diagram("test-2")

def test2():
    a = AutomataBuilder()
    [ a.add_state('q' + str(p)) for p in range(0, 4) ]
    a.set_initial('q0')

    a.add_transition('q0', '', 'q1')
    a.add_transition('q0', '0', 'q2')
    a.add_transition('q1', '0', 'q3')
    a.add_transition('q3', '', 'q2')
    a.add_transition('q1', '1', 'q2')
    a.add_transition('q2', '1', 'q2')

    a.generate_diagram("test-0")

    det = Determinizer(a)
    d = det.determinize()
    #~ d.generate_diagram("test-1")
    d = det.stringuify_set_states(d)
    d.generate_diagram("test-2")
    
def test1():
    a = AutomataBuilder()
    [ a.add_state('q' + str(p)) for p in range(0, 4) ]
    a.set_initial('q0')
    a.add_final('q0')
    a.add_final('q3')

    a.add_transition('q0', '0', 'q1')
    a.add_transition('q0', '1', 'q3')
    a.add_transition('q1', '0', 'q0')
    a.add_transition('q1', '1', 'q2')
    a.add_transition('q2', '0', 'q3')
    a.add_transition('q2', '1', 'q1')
    a.add_transition('q3', '0', 'q2')
    a.add_transition('q3', '1', 'q0')
    a.add_transition('q0', '', 'q3')

    det = Determinizer(a)
    print det.l_closure(set(['q0', 'q2']))

    #a.generate_diagram("test1")
    #da = DeterministicAutomata(a)

    #s = ''
    #print 'Parseando ' + s
    #if da.parse(s):
    #    print 'Sssssssssep'
    #else:
    #    print 'Nnnnup'

if __name__ == '__main__':
    test11()
